﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using EOS.DataModel;
using EOS.Common;

namespace EOS.EntWebDemo.Controllers
{
    public class ConditionsController : Controller
    {
        private DataModelContainer db = new DataModelContainer();

        public void BindViewBag()
        {
            SelectList nameList = new SelectList(ReflectionHelper.GetPropertyNames("Leave"));
            ViewBag.propertyNames = nameList.AsEnumerable();

            ViewBag.condtion = new List<SelectListItem>() {
                new SelectListItem(){Value="0",Text="="},
                new SelectListItem(){Value="1",Text=">"},
                new SelectListItem(){Value="2",Text="<"},
                new SelectListItem(){Value="3",Text=">="},
                new SelectListItem(){Value="4",Text="<="},
                new SelectListItem(){Value="5",Text="自定义"}
            };

            ViewBag.condtionRelation = new List<SelectListItem>() {
                new SelectListItem(){Value="0",Text="AND"},
                new SelectListItem(){Value="1",Text="OR"}
            };
        }

        // GET: Conditions
        //public ActionResult Index(int? id)
        //{
        //    var condition = db.Condition.Where(c => c.R_LeaveStep_PageInfoID == id);
        //    ViewBag.rID = id;
        //    return View(condition.ToList());
        //}

        // GET: Conditions/Create
        public ActionResult Create(int? rID)
        {
            Condition condition = db.Condition.First(c => c.R_LeaveStep_PageInfoID == rID);
            //Condition conditionNew = new Condition();
            //conditionNew.ID = condition.ID;
            //conditionNew.Relation = condition.Relation;
            //conditionNew.R_LeaveStep_PageInfoID = condition.R_LeaveStep_PageInfoID;
            //conditionNew.CondString = condition.CondString;
            BindViewBag();

            return View(condition);
        }

        // POST: Conditions/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 http://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Condition condition)
        {
            if (ModelState.IsValid)
            {
                if (db.Condition.Count() == 0)
                {
                    db.Condition.Add(condition);
                }
                else
                {
                    db.Entry(condition).State = EntityState.Modified;
                }
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return Create(condition.R_LeaveStep_PageInfoID);
        }

        //public ActionResult Delete(int? id)
        //{
        //    if (id == null)
        //    {
        //        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        //    }
        //    Condition condition = db.Condition.Find(id);
        //    if (condition == null)
        //    {
        //        return HttpNotFound();
        //    }
        //    return View(condition);
        //}

        // POST: Conditions/Delete/5
        //[HttpPost, ActionName("Delete")]
        //[ValidateAntiForgeryToken]
        //public ActionResult DeleteConfirmed(int id)
        //{
        //    Condition condition = db.Condition.Find(id);
        //    db.Condition.Remove(condition);
        //    db.SaveChanges();
        //    return RedirectToAction("Index");
        //}

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
